package com.lemote.dao.order;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import com.lemote.core.Page;
import com.lemote.core.PropertyFilter;
import com.lemote.entity.CircuitBoard;
import com.lemote.entity.FlowTask;
import com.lemote.orm.hibernate.HibernateDao;

/**
 * 
 * @author wuhui
 * @time 2011-7-19 下午08:59:25
 * @Version:0.0.1
 * @since smt
 */
@Component
public class CircuitBoardDao extends HibernateDao<CircuitBoard, Long> {

	/**
	 * @author wuhui
	 * @time 2011-7-24 下午09:36:57
	 * @Version:0.0.1
	 * @param page
	 * @param filters
	 * @param nowTasks
	 * @return
	 */
	public Page<CircuitBoard> searchBoard(Page<CircuitBoard> page,
			List<PropertyFilter> filters, FlowTask flowTask) {
		if (flowTask == null) {
			return page;
		}
		List<Criterion> criterionList = new ArrayList<Criterion>();
		for (PropertyFilter filter : filters) {
			if (!filter.hasMultiProperties()) { // 只有一个属性需要比较的情况.
				Criterion criterion = buildCriterion(filter.getPropertyName(),
						filter.getMatchValue(), filter.getMatchType());
				criterionList.add(criterion);
			} else {// 包含多个属性需要比较的情况,进行or处理.
				Disjunction disjunction = Restrictions.disjunction();
				for (String param : filter.getPropertyNames()) {
					Criterion criterion = buildCriterion(param, filter
							.getMatchValue(), filter.getMatchType());
					disjunction.add(criterion);
				}
				criterionList.add(disjunction);
			}
		}
		Criterion c = Restrictions.eq("flowTask", flowTask);
		criterionList.add(c);
		return findPage(page, criterionList.toArray(new Criterion[criterionList
				.size()]));
	}

	/**
	 * @param tasks
	 * @return
	 */
	public List<CircuitBoard> getAllBoard(List<FlowTask> tasks) {
		if (tasks==null||tasks.size()<=0) {
			return null;
		}
		Criterion[] criterionList = new Criterion[1];
		Criterion c = Restrictions.in("flowTask", tasks);
		criterionList[0] = c;
		return find(criterionList);
	}

}
